package com.chuji.crmcj.act;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.inputmethod.EditorInfo;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.RadioButton;
import android.widget.ScrollView;
import android.widget.TextView;
import android.widget.Toast;

import com.android.volley.Request;
import com.android.volley.RequestQueue;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.StringRequest;
import com.android.volley.toolbox.Volley;
import com.chuji.crmcj.R;
import com.chuji.crmcj.adapter.NiceSpinnerBaseAdapter;
import com.chuji.crmcj.utils.CommonUtil;
import com.chuji.crmcj.utils.SPUtils;
import com.chuji.crmcj.utils.UIUtils;
import com.chuji.crmcj.utils.UrlUtils;

import java.lang.reflect.Array;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Objects;

public class ClientInforActivity extends Activity implements View.OnClickListener{
    //返回
    private ImageView mIv_Back;
    //顾客姓名
    private EditText mEt_client_name;
    String client_name;
    //顾客手机号码
    private EditText mEt_client_phoneNumber;
    String phoneNumber;
    //顾客工作单位
    private EditText mEt_work_place;
    String work_place;
    //顾客家庭住址
    private EditText mEt_address;
    String address;
    //家庭情况
    private LinearLayout mLl_family_condition;
    private TextView tv_family_condition;
    //行业，职业
    private LinearLayout mLl_trade_profession;
    private TextView tv_trade;
    //兴趣爱好
    private LinearLayout mLl_interest;
    private TextView tv_interest;
    //保存
    private Button btn_save;
    //创建数组存储选中的数据
    List<Map<String,Object>> list;
    //意图
    private Intent mIntent;
    //请求码
    private int requestCode;

    ArrayList family,trade,interest;
    //客流ID
    String FlowID;
    //公司ID
    String CompanyID;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_client_infor);
        //初始化组件
        mIv_Back = (ImageView) findViewById(R.id.iv_Back);
        mEt_client_name = (EditText) findViewById(R.id.et_client_name);
        mEt_client_phoneNumber = (EditText) findViewById(R.id.et_client_phoneNumber);
        mEt_work_place = (EditText) findViewById(R.id.et_work_place);
        mEt_address = (EditText) findViewById(R.id.et_address);
        mLl_family_condition = (LinearLayout) findViewById(R.id.ll_family_condition);
        mLl_trade_profession = (LinearLayout) findViewById(R.id.ll_trade_profession);
        mLl_interest = (LinearLayout) findViewById(R.id.ll_interest);
        tv_family_condition= (TextView) findViewById(R.id.tv_family_condition);
        tv_trade= (TextView) findViewById(R.id.tv_trade);
        tv_interest= (TextView) findViewById(R.id.tv_interest);
        //弹出数字键盘
        mEt_client_phoneNumber.setInputType(EditorInfo.TYPE_CLASS_PHONE);
//        mEt_client_phoneNumber.addTextChangedListener(textWatcher);
        //返回
        mIv_Back.setOnClickListener(this);
        //家庭情况选择
        mLl_family_condition.setOnClickListener(this);
        //行业职业选择
        mLl_trade_profession.setOnClickListener(this);
        //兴趣爱好选择
        mLl_interest.setOnClickListener(this);
    }
    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        //购买用途返回数据
         family = data.getStringArrayListExtra("family");
        //购买用途返回数据
         trade = data.getStringArrayListExtra("trade");
        //购买用途返回数据
        interest = data.getStringArrayListExtra("interest");
        // 根据上面发送过去的请求码来区别
        switch (requestCode) {
            case 0:
                if(CommonUtil.ergodicArray(family).equals(" ")){
                    tv_family_condition.setText("未填");
                }else {
                    tv_family_condition.setText(CommonUtil.ergodicArray(family));
                }
                break;
            case 1:
                if(CommonUtil.ergodicArray(trade).equals(" ")){
                    tv_trade.setText("未填");
                }else {
                    tv_trade.setText(CommonUtil.ergodicArray(trade));
                }
                break;
            case 2:
                if(CommonUtil.ergodicArray(interest).equals(" ")){
                    tv_interest.setText("未填");
                }else {
                    tv_interest.setText(CommonUtil.ergodicArray(interest));
                }
                break;
            default:
                break;
        }

    }
    @Override
    public void onClick(View view) {
        switch (view.getId()){
            case R.id.iv_Back:
                if(client_name.equals("")||phoneNumber.equals("")){
                    UIUtils.showToastSafe("必填项还没有填写");
                } else if(phoneNumber.length()!=11){
                    UIUtils.showToastSafe("手机号码必须为11位");
                }else {
                    //传值，返回数据
                    Intent mIntent = new Intent();
                    mIntent.putExtra("Name", client_name);
                    mIntent.putExtra("Tel", phoneNumber);
                    mIntent.putExtra("WorkPlace", work_place);
                    mIntent.putExtra("HomeAddress", address);
                    mIntent.putExtra("HomeInfo", tv_family_condition.getText().toString());
                    mIntent.putExtra("Profession", tv_trade.getText().toString());
                    mIntent.putExtra("Hobbies", tv_interest.getText().toString());
                    // 设置结果，并进行传送
                    setResult(requestCode, mIntent);
                    finish();
                }
                break;
            case R.id.ll_family_condition:
                //跳转到家庭情况选择界面
//                showFamilyConditionDialog();
                mIntent = new Intent();
                mIntent.setClass(UIUtils.getContext(), FamilyConditionActivity.class);
                // 请求码的值随便设置，但必须>=0
                requestCode = 0;
                startActivityForResult(mIntent, requestCode);
                break;
            case R.id.ll_trade_profession:
                //跳转到行业选择界面
//                showTradeDialog();
                mIntent = new Intent();
                mIntent.setClass(UIUtils.getContext(), TradeProfessionActivity.class);
                // 请求码的值随便设置，但必须>=0
                requestCode = 1;
                startActivityForResult(mIntent, requestCode);
                break;
            case R.id.ll_interest:
                //跳转到兴趣爱好选择界面
//                showInterestDialog();
                mIntent = new Intent();
                mIntent.setClass(UIUtils.getContext(), InterestActivity.class);
                // 请求码的值随便设置，但必须>=0
                requestCode = 2;
                startActivityForResult(mIntent, requestCode);
                break;
//            case R.id.btn_save:
//                //保存顾客姓名
//                client_name=mEt_client_name.getText().toString();
//                //保存手机号码
//                phoneNumber=mEt_client_phoneNumber.getText().toString();
//                //保存工作单位
//                work_place=mEt_work_place.getText().toString();
//                //保存家庭住址
//                address=mEt_address.getText().toString();
//                if(client_name.equals("")||phoneNumber.equals("")){
//                    UIUtils.showToastSafe("必填项还没有填写");
//                }else {
//                    //提交数据到服务端
//                    Intent intent=getIntent();
//                    FlowID=intent.getStringExtra("FlowID");
//                    CompanyID=SPUtils.getString(UIUtils.getContext(),"CompanyID","");
//                    RequestQueue requestQueue = Volley.newRequestQueue(getApplicationContext());
//                    StringRequest stringRequest = new StringRequest(Request.Method.POST, UrlUtils.SUBMIT_CLEINT_INFO,
//                            new Response.Listener<String>() {
//                                @Override
//                                public void onResponse(String response) {
//                                    UIUtils.showToastSafe("保存成功");
//                                }
//                            }, new Response.ErrorListener() {
//                        @Override
//                        public void onErrorResponse(VolleyError error) {
//                            UIUtils.showToastSafe("保存失败，请检查网络");
//                        }
//                    }) {
//                        @Override
//                        protected Map<String, String> getParams() {
//                            //在这里设置需要post的参数
//                            Map<String, String> map = new HashMap<String, String>();
//                            map.put("FlowID",FlowID);
//                            map.put("CompanyID",CompanyID);
//                            map.put("Name",client_name);
//                            map.put("Tel",phoneNumber);
//                            map.put("WorkPlace",work_place);
//                            map.put("HomeAddress",address);
//                            map.put("HomeInfo",tv_family_condition.getText().toString());
//                            map.put("Profession",tv_trade.getText().toString());
//                            map.put("Hobbies",tv_interest.getText().toString());
//                            return map;
//                        }
//                    };
//                    requestQueue.add(stringRequest);
//                }
//
//                break;
            default:
                break;
        }
    }
//    private void showFamilyConditionDialog() {
//        AlertDialog.Builder builder = new AlertDialog.Builder(this);
//        final AlertDialog dialog = builder.create();
//        View view = View.inflate(this, R.layout.dialog_family_condition, null);// 给dialog设定特定布局
//        // dialog.setView(view);
//        dialog.setView(view, 0, 0, 0, 0);// 去掉上下左右边距, 兼容2.x版本
//        Button mBtSave = (Button) view.findViewById(R.id.btn_sure);
//        Button mBtCancel = (Button) view.findViewById(R.id.btn_cancel);
//
//        //Listview
//        final String[] beans = new String[] { "父母", "夫妻", "孩子", "在孕", "单身"};
//        ListView lv_checkBox= (ListView) view.findViewById(R.id.lv_checkBox);
//        ListViewAdapter adapter=new ListViewAdapter(ClientInforActivity.this, beans);
//        lv_checkBox.setAdapter(adapter);
//        lv_checkBox.setOnItemClickListener(new AdapterView.OnItemClickListener() {
//            @Override
//            public void onItemClick(AdapterView<?> adapterView, View view, int position, long l) {
//                list=new ArrayList<Map<String, Object>>();
//                Map<String, Object> map=new HashMap<String, Object>();
//                map.put("FamilyCondition",beans[position]);
//                list.add(map);
//            }
//        });
//        lv_checkBox.setChoiceMode(ListView.CHOICE_MODE_SINGLE);
//        mBtSave.setOnClickListener(new View.OnClickListener() {
//            @Override
//            public void onClick(View view) {
//                for (Map<String, Object> m : list) {
//                    for (String k : m.keySet()) {
//                        System.out.println(k + " : " + m.get(k));
//                        tv_family_condition.setText(m.get(k).toString());
//                    }
//                }
//
////                UIUtils.showToastSafe("保存成功");
//                //得到选中的数据并发送
//                dialog.dismiss();
//            }
//        });
//
//        mBtCancel.setOnClickListener(new View.OnClickListener() {
//            @Override
//            public void onClick(View v) {
//                dialog.dismiss();
//            }
//        });
//        dialog.show();
//    }
//
//    private void showTradeDialog() {
//        AlertDialog.Builder builder = new AlertDialog.Builder(this);
//        final AlertDialog dialog = builder.create();
//        View view = View.inflate(this, R.layout.dialog_family_condition, null);// 给dialog设定特定布局
//        // dialog.setView(view);
//        dialog.setView(view, 0, 0, 0, 0);// 去掉上下左右边距, 兼容2.x版本
//        Button mBtSave = (Button) view.findViewById(R.id.btn_sure);
//        Button mBtCancel = (Button) view.findViewById(R.id.btn_cancel);
//
//        //Listview
//        final String[] beans = new String[] {"教师", "职员", "律师", "公务员", "工人", "军人", "农户", "企业主"};
//        ListView lv_checkBox= (ListView) view.findViewById(R.id.lv_checkBox);
//        ClientTradeAdapter adapter=new ClientTradeAdapter(ClientInforActivity.this, beans);
//        lv_checkBox.setAdapter(adapter);
//        lv_checkBox.setOnItemClickListener(new AdapterView.OnItemClickListener() {
//            @Override
//            public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
//                UIUtils.showToastSafe("选中了"+i+"的"+beans[i]);
//            }
//        });
//        lv_checkBox.setChoiceMode(ListView.CHOICE_MODE_SINGLE);
//        mBtSave.setOnClickListener(new View.OnClickListener() {
//            @Override
//            public void onClick(View view) {
//                UIUtils.showToastSafe("保存成功");
//                //得到选中的数据并发送
//
//                dialog.dismiss();
//            }
//        });
//        mBtCancel.setOnClickListener(new View.OnClickListener() {
//            @Override
//            public void onClick(View v) {
//                dialog.dismiss();
//            }
//        });
//        dialog.show();
//    }
//    private void showInterestDialog() {
//        AlertDialog.Builder builder = new AlertDialog.Builder(this);
//        final AlertDialog dialog = builder.create();
//        View view = View.inflate(this, R.layout.dialog_family_condition, null);// 给dialog设定特定布局
//        // dialog.setView(view);
//        dialog.setView(view, 0, 0, 0, 0);// 去掉上下左右边距, 兼容2.x版本
//        Button mBtSave = (Button) view.findViewById(R.id.btn_sure);
//        Button mBtCancel = (Button) view.findViewById(R.id.btn_cancel);
//        final String[] beans = new String[] {"旅游", "运动", "垂钓", "越野", "唱歌"};
//        //Listview
//        ListView lv_checkBox= (ListView) view.findViewById(R.id.lv_checkBox);
//        ListViewAdapter adapter=new ListViewAdapter(ClientInforActivity.this, beans);
//        lv_checkBox.setAdapter(adapter);
//        lv_checkBox.setChoiceMode(ListView.CHOICE_MODE_SINGLE);
//        mBtSave.setOnClickListener(new View.OnClickListener() {
//            @Override
//            public void onClick(View view) {
//                UIUtils.showToastSafe("保存成功");
//                //得到选中的数据并发送
//
//                dialog.dismiss();
//            }
//        });
//        mBtCancel.setOnClickListener(new View.OnClickListener() {
//            @Override
//            public void onClick(View v) {
//                dialog.dismiss();
//            }
//        });
//        dialog.show();
//    }
    static class ListViewAdapter extends BaseAdapter {

        private Context context;
        private String[] beans;

        // 用来控制CheckBox的选中状况
        private static HashMap<Integer, Boolean> isSelected;
        class ViewHolder {
            TextView tvName;
            CheckBox cb;
        }
        public ListViewAdapter(Context context, String[] beans) {
            // TODO Auto-generated constructor stub
            this.beans = beans;
            this.context = context;
            isSelected = new HashMap<Integer, Boolean>();
            // 初始化数据
            initDate();
        }
        // 初始化isSelected的数据
        private void initDate() {
            for (int i = 0; i < beans.length; i++) {
                getIsSelected().put(i, false);
            }
        }
        @Override
        public int getCount() {
            // TODO Auto-generated method stub
            return beans.length;
        }
        @Override
        public Object getItem(int position) {
            // TODO Auto-generated method stub
            return beans[position];
        }
        @Override
        public long getItemId(int position) {
            // TODO Auto-generated method stub
            return position;
        }
        @Override
        public View getView(final int position, View convertView, ViewGroup parent) {
            // TODO Auto-generated method stub
            // 页面
            ViewHolder holder;
            String bean = beans[position];
            LayoutInflater inflater = LayoutInflater.from(context);
            if (convertView == null) {
                convertView = inflater.inflate(
                        R.layout.listview_family_condition, null);
                holder = new ViewHolder();
                holder.cb = (CheckBox) convertView.findViewById(R.id.cb_familycondition);
                holder.tvName = (TextView) convertView.findViewById(R.id.tv_device_name);
                convertView.setTag(holder);
            } else {
                // 取出holder
                holder = (ViewHolder) convertView.getTag();
            }
            holder.tvName.setText(bean);
            // 监听checkBox并根据原来的状态来设置新的状态
            holder.cb.setOnClickListener(new View.OnClickListener() {

                public void onClick(View v) {

                    if (isSelected.get(position)) {
                        isSelected.put(position, false);
                        setIsSelected(isSelected);
                    } else {
                        isSelected.put(position, true);
                        UIUtils.showToastSafe("选中了" + position + "的" + beans[position]);
                        setIsSelected(isSelected);
                    }

                }
            });
            // 根据isSelected来设置checkbox的选中状况
            holder.cb.setChecked(getIsSelected().get(position));
            return convertView;
        }

        public  HashMap<Integer, Boolean> getIsSelected() {
            return isSelected;
        }

        public  void setIsSelected(HashMap<Integer, Boolean> isSelected) {
            ListViewAdapter.isSelected = isSelected;
        }
    }
    /**
     * 给手机号加分隔符
     */
//    private final TextWatcher textWatcher = new TextWatcher() {
//        @Override
//        public void beforeTextChanged(CharSequence s, int start, int count, int after) {
//
//        }
//
//        @Override
//        public void onTextChanged(CharSequence s, int start, int before, int count) {
//            if (count == 1){
//                int length = s.toString().length();
//                if (length == 3 || length == 8){
//                    mEt_client_phoneNumber.setText(s + "-");
//                    mEt_client_phoneNumber.setSelection(mEt_client_phoneNumber.getText().toString().length());
//                }
//            }
//        }
//
//        @Override
//        public void afterTextChanged(Editable s) {
//
//        }
//    };
    //屏蔽系统返回键
    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if (keyCode == KeyEvent.KEYCODE_BACK) { //监控/拦截/屏蔽返回键

        }
        return false;
    }
    }
